package com.cloudtcm.controller.docker;

import com.cloudtcm.constant.JwtClaimsConstant;
import com.cloudtcm.dto.DockerDTO;
import com.cloudtcm.dto.DockerLoginDTO;
import com.cloudtcm.dto.EmployeeDTO;
import com.cloudtcm.entity.Docker;
import com.cloudtcm.properties.JwtProperties;
import com.cloudtcm.result.Result;
import com.cloudtcm.serviece.DockerService;
import com.cloudtcm.utils.JwtUtil;
import com.cloudtcm.vo.DockerLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/docker")
@CrossOrigin("*")
@Slf4j
@Api(tags = "医生相关接口")
public class DockerController {
    @Autowired
    private DockerService dockerService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 医生登录
     *
     * @param dockerLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("医生登录")
    public Result<DockerLoginVO> login(@RequestBody DockerLoginDTO dockerLoginDTO) {
        log.info("医生登录：{}", dockerLoginDTO);

        Docker docker = dockerService.login(dockerLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.DOCKER_ID, docker.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getSecretKey(),
                jwtProperties.getTtl(),
                claims);

        DockerLoginVO dockerLoginVO = DockerLoginVO.builder()
                .id(docker.getId())
                .userName(docker.getUsername())
                .image(docker.getImage())
                .phone(docker.getPhone())
                .direction(docker.getDirection())
                .garde(docker.getGrade())
                .comment(docker.getComment())
                .token(token)
                .build();

        return Result.success(dockerLoginVO);
    }

    /**
     * 医生注册
     * @param dockerDTO
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("医生注册")
    public Result save(@RequestBody DockerDTO dockerDTO){
        log.info("医生注册:{}", dockerDTO);

        dockerService.save(dockerDTO);
        return Result.success();
    }

    /**
     * 更新医生信息
     * @param dockerDTO
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("更新医生信息")
    public Result update(@RequestBody DockerDTO dockerDTO) {
        log.info("更新医生信息:{}", dockerDTO);
        dockerService.update(dockerDTO);
        return Result.success();
    }
}
